﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using MinGuiLuo;
using MinGuiLuo.Logging;
using UtilityLibrary.WinControls;

namespace WinFromDevelopFramework
{
    /// <summary>
    /// Library Module Plugins
    /// </summary>
    public partial class OEAEnvironment
    {
        #region  获取所有 Plugins
        public void StartupEntityPlugins(OutlookBar bar)
        {
            //查找 Module 目录下的所有程序集
            string modulePath = LEnvironment.MapDllPath("plugins\\");
            Directory.CreateDirectory(modulePath);
            listFileName = ParseDirectory(modulePath, "*.xml");
            foreach (string item in listFileName)
            {
                LogUtil.LogInfo(item);
                LoadMenus(item, bar);
            }
        }
        private ImageList imglist = new ImageList();
        private readonly string devimgfile = AppDomain.CurrentDomain.BaseDirectory + @"\Cnblogs.ico";
        public void LoadMenus(string xmlfile, OutlookBar bar)
        {
            XmlDocument document = new XmlDocument();
            document.Load(xmlfile);
            XmlNode rootNode = document.DocumentElement;
            string menutext = "";
            string imgfile = "";
            string assemblyname = "";


            foreach (XmlNode child in rootNode.ChildNodes)
            {
                if (child.NodeType == XmlNodeType.Element && "MenuItem".Equals(child.Name, StringComparison.InvariantCultureIgnoreCase))
                {

                    menutext = child.Name;
                    imgfile = child.Attributes.Name;
                    if (imgfile.Length == 0) imgfile = devimgfile;
                    imglist.Images.Add(Image.FromFile(imgfile));

                    OutlookBarBand outlookShortcutsBand = new OutlookBarBand(menutext);
                    for (int i = 0; i < child.ChildNodes.Count; i++)
                    {
                        menutext = child.ChildNodes[i].Attributes["Name"].Value;
                        assemblyname = child.ChildNodes[i].Attributes["AssemblyName"].Value;
                        imgfile = child.Attributes["Image"].Value;

                        outlookShortcutsBand.Items.Add(new OutlookBarItem(menutext, i));
                        outlookShortcutsBand.Tag = assemblyname;

                        if (imgfile.Length == 0) imgfile = devimgfile;
                        imglist.Images.Add(Image.FromFile(imgfile));

                    }
                    outlookShortcutsBand.SmallImageList = imglist;
                    outlookShortcutsBand.LargeImageList = imglist;
                    outlookShortcutsBand.Background = SystemColors.AppWorkspace;
                    outlookShortcutsBand.TextColor = Color.White;
                    bar.Bands.Add(outlookShortcutsBand);
                }
            }
        }
        static List<string> listFileName = new List<string>();
        private static List<string> ParseDirectory(string path, string filter)
        {//递归读取目录里所有文件(包括子目录）
            string[] dirs = Directory.GetDirectories(path);//得到子目录
            IEnumerator iter = dirs.GetEnumerator();
            while (iter.MoveNext())
            {
                string str = (string)(iter.Current);
                ParseDirectory(str, filter);
            }
            string[] files = Directory.GetFiles(path, filter);
            if (files.Length > 0)
            {
                listFileName.AddRange(files);
            }
            return listFileName;
        }
        #endregion
    }
}
